package com.geline.uploadfile.service;

import com.geline.uploadfile.pojo.JobUploadFileProperties;
import com.geline.uploadfile.pojo.UploadResult;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;

/**
 * 默认上传到本地实现类
 * @author: mengxin
 * @date: 2023/2/16 15:36
 */
public class LocalUploadService extends BaseUploadService{

    private JobUploadFileProperties properties;

    public LocalUploadService(JobUploadFileProperties properties){
        this.properties = properties;
    }

    public static String getHomePath(){
        ApplicationHome home = new ApplicationHome(LocalUploadService.class);
        File jarFile = home.getSource();
        return jarFile.getParentFile().getPath();
    }

    @Override
    public String getFileServerUrl() {
        return properties.getFileServerUrl();
    }

    @Override
    public String getBucketByLocal() {
        //---> /root/data/erp-stone/
        return properties.getBucket();
    }

    @Override
    public String getBucketByRemote() {
        return getBucketByLocal();
    }

    @Override
    public UploadResult uploadByRemote(String tenantId, MultipartFile multipartFile, String dirName) {
        return uploadByLocal(tenantId, multipartFile, dirName);
    }

    @Override
    public UploadResult uploadByRemote(String tenantId, String originalName, InputStream inputStream, String dirName) {
        return uploadByLocal(tenantId, originalName, inputStream, dirName);
    }

    @Override
    public UploadResult uploadByRemote(String tenantId, MultipartFile multipartFile, String dirName, String fileName) {
        return uploadByLocal(tenantId, multipartFile, dirName);
    }

    @Override
    public InputStream getFileByRemote(String bucket, String objectName) {
        return getFileByLocal(bucket, objectName);
    }

    @Override
    public boolean deleteFileByRemote(String bucket, String objectName) {
        return deleteFileByLocal(bucket, objectName);
    }
}
